<?php

namespace app\common\command;

use app\common\command\Base;
use app\common\model\AreaCode;
use app\common\model\Authentication;
use app\common\model\DemandDirectory;
use app\common\model\HelpOrderDay;
use app\common\model\setting\ConfigWeb;
use app\common\model\User;
use app\common\util\ExcelHelper;
use app\common\util\Result;
use think\console\input\Option;

class PatchUni extends Base
{
    protected function configure()
    {
        // 指令配置
        $this->setName('PatchUni')
            ->addOption('action', null, Option::VALUE_REQUIRED, 'action name')
            ->setDescription('统一修复工具类');
    }

    public function main()
    {
        $this->writeln("PatchUni begin");
        $action = $this->input->getOption("action");
        $this->writeln("action :" . $action);
        switch ($action) {
            case "user_area":
                //修复订单的学校数据
                //命令：php81 think PatchUni --action user_area
                $this->patchUserArea();
                break;
            case "import_help_order_day":
                //导入特派员时长数据
                //命令：php81 think ImportUni --action import_help_order_day

                break;
            default:
                $this->writeln("$action 未找到");
                break;
        }

        $this->writeln("PatchUni end");
    }

    private function patchUserArea()
    {
        $where = [];
        $where[] = ["area_code", "=", 0];
        $userList = User::with(["authentication"])->where($where)->select();
        $this->writeln("共找到：" . count($userList) . "条记录");
        foreach ($userList as $key => $user) {
            if ($user->authentication) {
                //如果有认证信息
                if (!$user->authentication->area_code) {
                    $user->authentication->autoFixedAreaCode();
                    $user->authentication->save();
                }
                $user->area_code = $user->authentication->area_code;
                $user->save();

                $this->writeln("更新".$user->real_name."：".$user->phone."的区域为：".$user->area_code);
            }
        }
    }


}